from rest_framework.views import exceptions
from aaa import models
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse

class MyAuthentication(object):
    def authenticate(self,request):
        # print('request._request',request._request.environ.get('HTTP_AUTHORIZATION'))
        # token = request._request.GET.get('token')
        # token = request._request.META.get('HTTP_AUTHORIZATION')
        token = request._request.environ.get('HTTP_AUTHORIZATION')
        print('auth token',token)
        token_obj = models.UserToken.objects.filter(token=token).first()
        print('auth token_obj', token_obj)
        if not token_obj:
            raise exceptions.AuthenticationFailed('用户认证失败')
            # return HttpResponse('账号已在其他地方登录')
            # return (111,222)
        return (token_obj.user,token)

    def authenticate_header(self,val):
        pass

class FirstAuthentication(object):
    def authenticate(self,request):
        pass

    def authenticate_header(self,val):
        pass

class MyPermission(object):
    def has_permission(self,request,view):
        if request._request.user.user_type != 0:
            return False
        return True